<?php 
global $current_user ;
get_currentuserinfo();
if( current_user_can('editor') || current_user_can('administrator') ){
$args=array(
    'post_type' => 'post',
    'post_status' => 'publish, private, draft',
    'posts_per_page' => 20,
    'caller_get_posts'=> 1,
	'paged' => $paged
  );
}
else {$args=array(
    'author' => $current_user->ID,
    'post_type' => 'post',
    'post_status' => 'publish, private, draft',
    'posts_per_page' => 10,
    'caller_get_posts'=> 1,
	'paged' => $paged
  );}
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; 
  
  $my_query = null;
  $my_query = new WP_Query($args);
if (current_user_can('author')){
  $user_post_count = count_user_posts_by_status('draft', $current_user->ID);
  $user_post_count2 = count_user_posts_by_status('publish', $current_user->ID);
  $countpost =  $user_post_count + $user_post_count2;
  if ( $countpost<1){ ?>
  </br></br>
 <strong> Kamu belum membuat cerita apa pun.
  </br>
  Mulailah membuat cerita baru dan sebarkan ceritamu. </strong>
  <?php
  exit;
  }}
 // else{
  
  if( $my_query->have_posts() ) {
   
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
     
<div class="row">
<?php 
						$thumb_id = get_post_thumbnail_id();
						$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'recent-thumbnails', true);
						$thumb_url = $thumb_url_array[0];
						$bookid = get_the_id(); 
		?>
		
    <div class="col-md-2"><img src="<?php echo $thumb_url ?>"  class="imgukuran"><br/>&nbsp;<a href="<?php echo get_site_url()?>/editsampul?thumburl=<?php echo $thumb_url?>&thumbid=<?php echo $thumb_id?>&bookid=<?php echo $bookid?>" id="EditSampul"><button type="button" class="btn btn-xs">Edit Sampul</button></a> <br/>
	
							<br/>
							
							</div>
                            <div class="col-md-10 textrecentpost">
							<span class="glyphicon glyphicon-time font10"></span>&nbsp; <?php the_time("l, d F Y");?>  <?php the_time("g:i a"); ?> 
                                <table style="width:100%">
                                <tr>
                                <td style="width:70%">
								
                                <h8>
                                    <a class="fontjudul" href="<?php if (get_post_status()=='draft'){ echo get_site_url()."/draftpriview?bookid=".$bookid;} else {the_permalink();} ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); if (get_post_status()=='draft') echo " (Konsep) "?></a>
                                
								</h8>
								</td>
								<td>
									<div style="text-align:right; font-size:12px;"><?php if( current_user_can('delete_others_posts') || current_user_can('manage_options') ) {echo "oleh: " ; the_author_posts_link();}?></div>
								</td>
								</tr>
								<tr>
								<td style="font-size:11px;">
                                <!--<span class="glyphicon glyphicon glyphicon-time"></span><a class="font2">&nbsp;May 6th, 2014&nbsp;&nbsp;</a>-->
                                <span class="glyphicon glyphicon glyphicon-tags font2"></span><a class="font2" >&nbsp;&nbsp;<?php the_tags("", ", ", "");?>&nbsp;&nbsp;</a>
                                <span class="glyphicon glyphicon-comment font2"></span><a class="font2">&nbsp;<?php comments_number( '0', '1', '% ' );?>  Comments&nbsp;&nbsp;</a>
                                <span class="glyphicon glyphicon-eye-open font2"></span><a class="font2">&nbsp;<?php the_views()?> Views</a>
                                <div>
                                	<?php the_ratings();?>
                                </div>
                                </td>
                                <td style="font-size:12px;">
                                <a href="<?php echo get_site_url()?>/kelolacerita?bookid=<?php echo $bookid?>&edit=yes" id="KelolaCerita"><span class="glyphicon glyphicon-cog"></span>&nbsp;Kelola</a> | <a href="<?php echo get_site_url()?>/babbaru?bookid=<?php echo $bookid; ?>&addnew=yes"><span class="glyphicon glyphicon-pencil"></span>&nbsp;Tambah Bab</a> | <a href='<?php echo $bookid ?>' id='HapusBuku'><span class='glyphicon glyphicon-trash'></span>&nbsp;Hapus </a>
                                </td>
                                </tr>
                                </table>
                               
								<?php	$mykey_values = get_post_custom_values( 'Chapter' );
								if (count($mykey_values) >0){
								$sort = array();
								$idbab = array();
                                foreach ( $mykey_values as $key => $babid ) 
										{
											
											$number = get_post_meta($babid, 'number', true);//get_post_custom_values ( 'number', $babid);
											array_push($sort, (int)$number);
											array_push($idbab, $babid);
										
												
										}
										//sorting chapter berdasarkan urutan (number)
											array_multisort($sort,$idbab);
											//var_dump($sort);
											//var_dump($idbab);
								//return array yang sudah di sorting
								 foreach ( $idbab as $sortedid => $babidsorted )
								 {
								    $chapter = get_post($babidsorted);
									$title = $chapter->post_title;
									$content = $chapter -> post_content;
									$status = $chapter->post_status;
									$link = get_permalink($bookid)."?pid=".$babidsorted . "&" .$title;
									$linkdraft = get_site_url() . "/draftpriview?bookid=".$bookid."&pid=".$babidsorted . "&" .$title ."&priview&edit=yes";
									$link2 = get_site_url()."/book/babbaru?bookid=".$bookid."&pid=".$babidsorted . "&" .$title ."&edit=yes";
										if ($status=='draft'){
												echo "<a href='$linkdraft'>".$title ."</a> (Konsep) <div style='float:right'> <a  href='$link2'><span class='glyphicon glyphicon-edit'></span>&nbsp;Ubah&nbsp;</a> | <a href='$babidsorted|$bookid' id='HapusBab'><span class='glyphicon glyphicon-trash'></span>&nbsp;Hapus </a></div>" . "<br/>";
														}
										else {
												echo "<a href='$link'>".$title ."</a><div style='float:right'> <a  href='$link2'><span class='glyphicon glyphicon-edit'></span>&nbsp;Ubah&nbsp;</a>| <a href='$babidsorted|$bookid' id='HapusBab'><span class='glyphicon glyphicon-trash'></span>&nbsp;Hapus</a> </div>" . "<br/>";
													}
								 } 			
											 
								}
								
else {echo "<p style='color:purple'>Belum ada bab ditemukan, mulailah menulis bab baru</p>";}											?>
                                <hr/>
                            </div>
							</div>
                 
						 
      <?php
    endwhile;
	//wp_pagenavi();
	wp_pagenavi(array('query'=>$my_query))?>
<?php  }//}
  wp_reset_query();  // Restore global post data stomped by the_post().

?>